<script setup>
import { ref, onMounted } from 'vue'
import VditorPreview from '@/components/VditorPreview.vue'
import { getArticleDetail } from '@/api/article'
import { useRoute, useRouter } from 'vue-router'
import { Back } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()

const article = ref({})
// 获取文章详情
const getArticle = async (id) => {
  const res = await getArticleDetail({ id })
  if (res.code !== 200) return
  article.value = res.data
  document.title = `${article.value.title} - 鼠觅`
}

// 获取文章详情
onMounted(async () => {
  route.query.id && (await getArticle(parseInt(route.query.id)))
})
</script>

<template>
  <div class="article-page">
    <div class="header">
      <div class="title">
        <h1>{{ article.title }}</h1>
      </div>
      <div class="btn">
        <el-button plain @click="router.push('/')" :icon="Back" size="small"></el-button>
      </div>
    </div>
    <div class="main">
      <VditorPreview v-model="article.content" />
    </div>
  </div>
</template>

<style scoped lang="scss">
.article-page {
  height: 100%;
  position: relative;
  padding-top: 50px;

  .header {
    height: 50px;
    width: 100%;
    display: flex;
    align-items: center;
    border-bottom: 1px solid var(--el-border-color);
    position: fixed;
    top: 0;
    z-index: 999;
    background-color: var(--page-bg-color);

    .title {
      height: 100%;
      padding: 0 20px;
      flex: 1;
      overflow: hidden;

      h1 {
        font-size: 24px;
        font-weight: normal;
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        white-space: nowrap;
      }
    }
    .btn {
      padding: 0 20px;
    }
  }
  .main {
    height: calc(100% - 50px);
  }
}
</style>
